window.onload = function () {
    /*var map = new AMap.Map('container', {
        resizeEnable: true,
        zoom: 12,
        center: [118.756376, 32.052573]
    });*/
	var map, geolocation;
	//经度： 纬度：
	var pointLng,pointLat;
		
    //加载地图，调用浏览器定位服务
    map = new AMap.Map('container', {
        resizeEnable: true,
			zoom: 12
    });
	//AMap.getUiSettings().setCompassEnabled(true);  
    //地图内容
    map.setFeatures(['bg', 'building', 'road', 'point'])
    map.plugin(['AMap.Geolocation','AMap.ToolBar', 'AMap.Scale', 'AMap.MapType'], function() {
        geolocation = new AMap.Geolocation({
            enableHighAccuracy: true,//是否使用高精度定位，默认:true
            timeout: 10000,          //超过10秒后停止定位，默认：无穷大
            buttonOffset: new AMap.Pixel(10, 50),//定位按钮与设置的停靠位置的偏移量，默认：Pixel(10, 20)
            zoomToAccuracy: true,      //定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
            buttonPosition:'LB'
        });
        map.addControl(geolocation);
		map.addControl(new AMap.ToolBar());
        map.addControl(new AMap.Scale());
        geolocation.getCurrentPosition();
        AMap.event.addListener(geolocation, 'complete', onComplete);//返回定位信息
        //AMap.event.addListener(geolocation, 'error', onError);      //返回定位出错信息
    });

	 //解析定位结果
    function onComplete(data) {
        pointLng=data.position.getLng();
        pointLat=data.position.getLat();  
		//console.log(JSON.stringify(data));

    /*var marker = new AMap.Marker({
        position: [pointLng, pointLat]
    });
    marker.setMap(map);
    var circle = new AMap.Circle({
        center: [pointLng, pointLat],
        radius: 100,
        fillOpacity: 0.2,
        strokeWeight: 1
    })
    circle.setMap(map);*/
//自定义窗体
    var infowindow;
    var infoWindowContent = '<div class="infowindow-content"><h3>当前位置</h3><p>' + data.formattedAddress + '</p></div>';
    map.plugin('AMap.AdvancedInfoWindow', function () {
        infowindow = new AMap.AdvancedInfoWindow({
            panel: 'panel',
            placeSearch: true,
            asOrigin: true,
            asDestination: true,
            content: infoWindowContent
        });
        infowindow.open(map, [pointLng, pointLat]);
    });
    }

//地图空间
   /* AMap.plugin(['AMap.ToolBar', 'AMap.Scale', 'AMap.MapType'],
        function () {
            map.addControl(new AMap.ToolBar());

            map.addControl(new AMap.Scale());
        });*/
//覆盖物
    

//汽车路线规划
    $('#car').on('click', function () {
        $('.pageShow').slideToggle();
        clearMarker()
        AMap.plugin('AMap.Driving', function () {
            var drving = new AMap.Driving({
                map: map,
                panel: "panel"
            })
            drving.search([
                {keyword: $('#star').val()},
                {keyword: $('#end').val()}
            ]);
        })
    })
//步行路线规划
    $("#riding").on('click', function () {
        $('.pageShow').slideToggle();
        clearMarker()
        var walking = new AMap.Walking({
            map: map,
            panel: "panel"
        });
        walking.search([
            {keyword: $('#cStar').val()},
            {keyword: $('#cEnd').val()}
        ]);
    })
//骑行路线规划
    $('#walk').on('click', function () {
        $('.pageShow').slideToggle();
        clearMarker()
        var riding = new AMap.Riding({
            map: map,
            panel: "panel"
        });
        riding.search([
            {keyword: $('#wStar').val()},
            {keyword: $('#wEnd').val()}
        ]);
    })

    function clearMarker() {
        if (marker) {
            marker.setMap(null);
            marker = null;
        }
        if (infowindow) {
            infowindow.close()
        }
    }

//输入提示
    var autoOptions = new AMap.Autocomplete({
        input: "tipinput"
    });
//城市搜索
    var auto = new AMap.Autocomplete(autoOptions);
    var placeSearch = new AMap.PlaceSearch({
        map: map
    });  //构造地点查询类
    AMap.event.addListener(auto, "select", select);//注册监听，当选中某条记录时会触发
    function select(e) {
        placeSearch.setCity(e.poi.adcode);
        placeSearch.search(e.poi.name);  //关键字查询查询
    }

    $('#show').on('click', function () {
        clearMarker()
        $('.pageShow').slideToggle();
    })

    $('#box').on('click', function () {
        clearMarker()
        $('.pageShow').slideToggle();
        var rectOptions = {
            strokeStyle: "dashed",
            strokeColor: "#333",
            fillColor: "#333",
            fillOpacity: 0.3,
            strokeOpacity: 1,
            strokeWeight: 1
        };
        map.plugin(["AMap.MouseTool"], function () {
            var mouseTool = new AMap.MouseTool(map);
            //通过rectOptions更改拉框放大时鼠标绘制的矩形框样式
            mouseTool.rectZoomIn(rectOptions);
        });
    })   
}
